package server;

import java.util.HashMap;
import java.util.Optional;

public class Host {

    private String name;

    /**
     * 存放context名称和context映射关系
     */
    private HashMap<String, Context> contextMap = new HashMap<>();

    /**
     * 部署目录的绝对路径
     */
    private String appBase;

    public Host(String name, String appBase) {
        this.name = name;
        this.appBase = appBase;
    }

    public void addContext(String name, Context context){
        contextMap.put(name, context);
    }

    public HashMap<String, Context> getContextMap() {
        return contextMap;
    }

    public void setContextMap(HashMap<String, Context> contextMap) {
        this.contextMap = contextMap;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAppBase() {
        return appBase;
    }

    public void setAppBase(String appBase) {
        this.appBase = appBase;
    }

    public Servlet getServlet(String url) {
        Context context = contextMap.get(url.split("/")[1]);

        return Optional.ofNullable(context).map(ctx -> ctx.getServlet(url)).orElse(null);
    }
}
